;
define(function() {

    var navigationMenu;
    if (navigationMenu) {
        navigationMenu = null;
    }

    navigationMenu = {
        init: function() {
            this.bindEvents();
            this.getNavigationMenu();
        },
        bindEvents: function() {
            //状态筛选
            $(".task-i-h-ul li span").on("click", function() {
                $(".task-i-h-ul li span").removeClass("i-h-choose");
                $(this).addClass("i-h-choose");

                var state = $(this).attr("state");
                var $ul = $(this).closest(".task-info").find(".task-info-list .task-i-l-ul");
                $ul.find("li").show();

                if (state != "") {
                    $ul.find("li").not($ul.find("li[state='" + state + "']")).hide();
                }
            });

            //任务链表
            $(".task-i-l-ul li i").on("click", function() {
                $(".task-i-l-ul li").removeClass("i-l-ul-li");
                $(".task-i-l-ul li").find(".task-s-active").removeClass("task-s-a-choose");
                $(this).parent().addClass("i-l-ul-li");
                $(this).parent().find(".task-s-active").addClass("task-s-a-choose");
            });
            $(".state>span").click(function() {
                $(this).addClass("active").siblings().removeClass("active")
            })
            this.xxBindEvents();

            //去蓝底
            $(".nav").on("selectstart", function() {
                return false;
            });
        },
        //任务链表向下展开
        xxBindEvents: function() {
            $(".task-menu").click(function() {
                $(this).parent().siblings().find(".task-menu-content").hide();
                $(this).parent().siblings().find("i").removeClass("active");
                if ($(this).next(".task-menu-content:hidden").length === 0) {
                    $(this).next(".task-menu-content").hide();
                } else {
                    $(this).next(".task-menu-content").show();
                };
                if ($(this).find("i.shrink").hasClass("active")) {
                    $(this).find("i.shrink").removeClass("active");
                } else {
                    $(this).find("i.shrink").addClass("active");
                };
            });
        },
        //获取菜单信息
        getNavigationMenu: function() {
            var that = this;
            var userInfo = $("#userInfo").data('info');
            $.ajax({
                type: 'GET',
                url: api.internal.common.getTodayTaskList,
                data: { userId: userInfo.smuser.iid },
                dataType: "json",
                async: false,
                success: function(result) {
                    that.nmInfo = result.data;

                    that.taskSSH = []; //定时任务
                    that.taskUTH = []; //不定时任务

                    //后台传过来的所有任务区分开
                    if (that.nmInfo && that.nmInfo.length > 0) {
                        for (var i = 0; i < that.nmInfo.length; i++) {
                            var obj = that.nmInfo[i];
                            if (obj) {
                                if (obj.taskType === 0) {
                                    //定时任务
                                    that.taskSSH.push(obj);
                                } else if (obj.taskType == 1) {
                                    //不定时任务
                                    that.taskUTH.push(obj);
                                }
                            }
                        }
                    }
                    that.taskSSH.sort(function(i, j) {
                        var prevTime = new Date(i.duty_day + " " + i.fortime);
                        var nextTime = new Date(j.duty_day + " " + j.fortime);
                        return prevTime.getTime() - nextTime.getTime()
                    });
                    that.getMontage();
                    $("#timedTask ul.task-menu-list li").first().trigger('click');
                }
            });
        },
        //拼接菜单
        getMontage: function() {
            //定时任务
            if (this.taskSSH.length > 0) {
                $("#timedTask .task-menu-list").empty();

                for (var i = 0; i < this.taskSSH.length; i++) {
                    var data = this.taskSSH[i];
                    var stateStyle = this.getStateStyle(data.taskState);
                    var $li =
                        $('<li class="' + stateStyle + '" data-href="' + (data.productUrl || 'undefind') + '" data-target="_self">' +
                            '<i></i>' +
                            '<span>' + data.fortime + '</span>' +
                            '<a>' + data.taskName + '</a>' +
                            '</li>'
                        );
                    $("#timedTask .task-menu-list").append($li);
                }
            }

            //不定时任务
            if (this.taskUTH.length > 0) {
                $("#noTask .task-menu-list").empty();

                for (var i = 0; i < this.taskUTH.length; i++) {
                    var data = this.taskUTH[i];
                    var $li =
                        $('<li data-href="' + data.productUrl + '" data-target="_self">' +
                            '<span>' + data.taskName + '</span>' +
                            '</li>'
                        );
                    $("#noTask .task-menu-list").append($li);
                }
            }
        },
        //为任务状态添加样式
        getStateStyle: function(state) {
            var result = "task-info-notmake";
            if (state == 1) {
                result = "task-info-accomplish";
            } else if (state == 2) {
                result = "task-info-overtime";
            }

            return result;
        }
    }
    return {
        init: function() {
            navigationMenu.init();
        }
    }
});